package com.nf.health.app.service;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.telephony.TelephonyManager;

import com.nf.health.app.Constants;
import com.nf.health.app.utils.LogUtil;
import com.nf.health.app.utils.PreferenceHelper;

public class MyMsgReceiver extends BroadcastReceiver {

    private static final String TAG = MyMsgReceiver.class.getSimpleName();
    private String incoming_number;
    /**
     * 消息，社交等Action
     */
    public static final String ACTION_NLS_CONTROL = "com.nf.health.app.NLSCONTROL";
    /**
     * 监听短信的ACTION
     */
    final String ACTION_SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
    /**
     * 电话监听
     */
    final String ACTION_CALL_RECEIVED = TelephonyManager.ACTION_PHONE_STATE_CHANGED;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (ACTION_CALL_RECEIVED.equals(intent.getAction())) {//电话
            if (PreferenceHelper.getBoolean(PreferenceHelper.isCallON,
                    false)) {
                TelephonyManager tm = (TelephonyManager) context
                        .getSystemService(Service.TELEPHONY_SERVICE);
                switch (tm.getCallState()) {
                    case TelephonyManager.CALL_STATE_RINGING://响铃
                        incoming_number = intent.getStringExtra("incoming_number");
                        if (Constants.mBluetoothLeService != null) {
                            Constants.mBluetoothLeService.setPushCallName(incoming_number);
                            LogUtil.i(TAG, "设置来电震动 :");
                        }
                        LogUtil.i(TAG, "RINGING :" + incoming_number);
                        break;
                    case TelephonyManager.CALL_STATE_OFFHOOK://摘机
                        LogUtil.i(TAG, "incoming ACCEPT :" + incoming_number);
                        break;
                    case TelephonyManager.CALL_STATE_IDLE://无呼入或已挂断
                        int num = intent.getExtras().getInt("MissedCallNumber");//得到结果总是0
                        LogUtil.i(TAG, "incoming IDLE num=" + num);
                        if (Constants.mBluetoothLeService != null) {
//						Constants.mBluetoothLeService.setPushUCallNum(num);//三星手机,来电就收到此广播,与响铃一起设置导致响铃设置失败 暂时先注释掉 2016/12/20
                        }
                        break;
                }
            }
        } else if (ACTION_SMS_RECEIVED.equals(intent.getAction())) {
            if (PreferenceHelper.getBoolean(PreferenceHelper.isSMSON, false)) {

                if (Constants.mBluetoothLeService != null) {
                    int num = getNewSmsCount(context);
                    Constants.mBluetoothLeService.setPushSMSNum(num);
                    LogUtil.i(TAG, "设置短信震动 :"+num);
                }
            }
        } else if (ACTION_NLS_CONTROL.equals(intent.getAction())) {
            if (PreferenceHelper.isSocialON.equals(intent.getStringExtra("type"))) {
                if (Constants.mBluetoothLeService != null) {
					Constants.mBluetoothLeService.setPushUSocNum(1);
                    LogUtil.i(TAG, "设置社交震动 :");

//                    Constants.mBluetoothLeService.setPushSMSNum(1);
//                    LogUtil.i(TAG, "设置短信震动 :"+1);
                }
            }
            if (PreferenceHelper.iscalendarON.equals(intent.getStringExtra("type"))) {
                if (Constants.mBluetoothLeService != null) {
                    Constants.mBluetoothLeService.setPushUCalNum(1);
                    LogUtil.i(TAG, "设置日历震动:");
                }
            }
        }

    }

    private int getNewSmsCount(Context context) {
        int result = 0;
        Cursor csr = context.getContentResolver().query(Uri.parse("content://sms"), null,
                "type = 1 and read = 0", null, null);
        if (csr != null) {
            result = csr.getCount();
            csr.close();
        }
        return result;
    }
}
